package android.SettingsTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Settings {

	// private Context mCtx;
	// private Handler mHandler;
	// private WebView mAppView;
	private static HashMap<String, String> Text;
	private File f;

	public Settings(File f){
		this.f = f;
	}

	public String getValue(String value) {
		System.out.println("getvalue " + value);
		String text = getText().get(value);
		if (text != null) {
			return text;
		} else {
			return "unknown";
		}
	}

	public void setValue(String name, String value) {
		System.out.println("setvalue " + name + " " + value);
		if (name != null && value != null) {
			getText().put(name, value);
		}
	}

	public void update() throws ParserConfigurationException, FileNotFoundException {
		
		
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document document = documentBuilder.newDocument();

		String root = "widget:preferences";
		Element rootElement = document.createElement(root);
		document.appendChild(rootElement);

		Collection<String> c = getText().keySet();
		Iterator<String> i = c.iterator();

		while (i.hasNext()) {
			Element em = document.createElement("preference");
			String key = i.next();
			em.setAttribute("name", key);
			em.setAttribute("type", "text");
			em.setAttribute("defaultValue", getText().get(key));
			rootElement.appendChild(em);
		}
		
		FileOutputStream fOut = new FileOutputStream(f);
		OutputStreamWriter osw = new OutputStreamWriter(fOut);
        try {
        	System.out.println(getXml(document));
			osw.write(getXml(document));
			osw.flush(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            try {
                osw.close();
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
		} 
	}
	
	public String getXml(Node node) {
		StringBuilder buffer = new StringBuilder();

		if (node == null) {
			return "";
		}

		if (node instanceof Document) {
			buffer.append(getXml(((Document) node).getFirstChild()));
		} else if (node instanceof Element) {
			Element element = (Element) node;
			buffer.append("<");
			buffer.append(element.getNodeName() + " ");
			if (element.hasAttributes()) {
				NamedNodeMap map = element.getAttributes();
				for (int i = 0; i < map.getLength(); i++) {
					Node attr = map.item(i);
					buffer.append(attr.getNodeName());
					buffer.append("=\"");
					buffer.append(attr.getNodeValue());
					buffer.append("\" ");
				}
			}
			buffer.append(">");
			NodeList children = element.getChildNodes();
			for (int i = 0; i < children.getLength(); i++) {
				buffer.append(getXml(children.item(i)));
			}
			buffer.append("</");
			buffer.append(element.getNodeName());
			buffer.append(">");
		} else if (node != null && node.getNodeValue() != null) {
			buffer.append(node.getNodeValue());
		}

		return buffer.toString();
	}

	public void setText(HashMap<String, String> text) {
		Text = text;
	}

	public HashMap<String, String> getText() {
		return Text;
	}
}
